<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Result output</title>
</head>
<?php 
	include("connect.php");
    
   /* function create_complaint($userID, $sessionID, $complaint, $pending) {
        $sql_create_row = "INSERT INTO complaints (idNum, sessionID, complaint, pending) VALUES ('$userID', '$$sessionID', '$complaint', '$pending')";
		mysql_query($sql_create_row);
        return mysql_insert_id();
    }*/
    
    function create_complaint() {
        $complaint_id = -1;
        if (isset($_POST["complaints_complaint"]))
	    {
		    $userID = $_POST["users_idNum"];
		    $userID = intval($userID);
		    $complaint = $_POST["complaints_complaint"];
		    $ses_id = session_id();
		    $now = date("Y-m-d H:i:s");
	
		    //Check for session in data base and update or create
		    $sessionExists = mysql_query("SELECT * FROM sessions WHERE idNum = '$userID' and sessionID='$ses_id'");
		    $num_rows = mysql_num_rows($sessionExists);
		    if ( $num_rows == 0 )
		    {
			    $sql_create_row = "INSERT INTO sessions (idNum, sessionID, date) VALUES ('$userID', '$ses_id', '$now')";
			    mysql_query($sql_create_row);
		    }
	
		    //Check for complaint in dataBase or create new entry
            $complaintExists = mysql_query("SELECT * FROM complaints WHERE idNum = '$userID' and sessionID='$ses_id' and complaint='$complaint'");
		    $num_rows = mysql_num_rows($complaintExists);
		    if ( $num_rows == 0 )
		    {
               $sql_create_row = "INSERT INTO complaints (idNum, sessionID, complaint, pending) VALUES ('$userID', '$ses_id', '$complaint', '1')";
			    mysql_query($sql_create_row);
                $complaint_id = mysql_insert_id();
		    }
           
		    //Check for complaint type in DB and update
		    $typeExists = mysql_query("SELECT * FROM $complaint WHERE idNum = '$userID' and sessionID='$ses_id'");
		    $num_rows = mysql_num_rows($typeExists);
		    if ( $num_rows == 0 )
		    {
			    $sql_create_row = "INSERT INTO $complaint (idNum, sessionID) VALUES ('$userID', '$ses_id')";
			    mysql_query($sql_create_row);
		    }
		
		    //Populate or update tables with data
		    foreach($_POST as $key => $value)
		    {
			    if ((isset($_POST[$key])) and ($value!=NULL))
			    {
				    $pieces = explode("_", $key);
				    $tableName = $pieces[0];
				    $key = $pieces[1];
				    if ($tableName == $complaint)
				    {
                        if (is_array($value))
                        {
                            $value = implode(", ", $value);
                        }
					    $sql_update = "UPDATE $tableName SET $key='$value' WHERE idNum='$userID' and sessionID='$ses_id'";
					    mysql_query($sql_update);
				    }
			    }
		    }
	    }
        return $complaint_id;        
    }
?>

<body>
</body>
</html>